﻿//获取浏览器参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

var state = 0;

$(function () {
    $('#input_ssje').focus();  //当前页面输入框选中

    var sl = getQueryString("sl");
    var je = getQueryString("je");

    $('.w130').text("合计数量：" + sl);
    $('.w180').text("合计金额：" + je);

    $('#je').html(je);
    $('#collection').html(je);
    $('#amountofmoney').html(je);


    //主页面快捷键
    $(document).on('keydown', function (e) {
        switch (e.keyCode) {
            case 115:    //F4 银联卡支付
                e.preventDefault();
                payment('payment.html?t=2&collection=' + $('#collection').html() + '&money=' + $('#je').html(), '360px', '300px');
                break;
            case 116:   //F5 微信支付
                e.preventDefault();
                payment('payment.html?t=1&collection=' + $('#collection').html() + '&money=' + $('#je').html(), '360px', '300px');
                break;

            case 119:   //F8 支付宝
                e.preventDefault();
                payment('payment.html?t=3&collection=' + $('#collection').html() + '&money=' + $('#je').html(), '360px', '300px');
                break;

            case 13:    //回车
                e.preventDefault();
                var input_ssje = parseFloat($('#input_ssje').val()),    //实收
                    payType = $('#payType').val(); // 支付方式
                if (payType != 1 && !input_ssje) {
                    message('message.html?t=8', '300px', '200px');
                }
                else {
                    var collection = parseFloat($('#collection').text());   //代收
                    if (state == 1) {
                        if (input_ssje >= collection) {
                            var uid = getQueryString("id");
                            var authCode = "";
                            var cardAmount = $('#je').html();
                            var amount = collection;
                            var payType = amount > 0 ? 1 : 6;
                            paymentMethod(uid, authCode, cardAmount, amount, payType, je);
                            var givechange = parseFloat(input_ssje) - collection;
                            $('#givechange').text(numFloat(givechange));                //找零
                        }
                        else {
                            message('message.html?t=9', '300px', '200px');
                        }
                    }
                    else {
                        var givechange = parseFloat(input_ssje) - collection;
                        if (givechange < 0) {
                            message('message.html?t=9', '300px', '200px');
                        }
                        else {
                            var uid = getQueryString("id");
                            var authCode = "";
                            var cardAmount = $('#je').html();
                            var amount = collection;
                            var payType = 1;
                            paymentMethod(uid, authCode, cardAmount, amount, payType, je);
                            $('#givechange').text(numFloat(givechange));                //找零
                        }
                    }
                }
                break;
            case 27:        //ESC
                e.preventDefault();
                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
                parent.$('#sku').focus();
                break;
            case 120:
                e.preventDefault();
                state = 1;
                var uid = $.cookie('uid');
                if (uid != "" && uid != null) {
                    var m = parseFloat(getQueryString("m"));
                    $('#key').html("【F9】");
                    $('#zftype').html("会员卡");
                    var amountofmoney = parseFloat($('#amountofmoney').html()); //实收
                    if (m < amountofmoney) {
                        $('#je').html(m);
                        var ds = amountofmoney - m;
                        $('#collection').html(numFloat(ds));
                    }
                    else {
                        $('#je').html(amountofmoney);
                        $('#collection').html(numFloat("0.00"));
                    }
                    $('#payType').val(1);
                }
                break;

            case 112:
                e.preventDefault();
                collection = $('#collection').html();
                state = 0;
                $('#key').html("【F1】");
                $('#zftype').html("人民币");
                $('#jg').html(collection);
                var amountofmoney = parseFloat($('#amountofmoney').html()); //实收
                $('#collection').html(amountofmoney);
                break;
        }
    })
});
//制保留2位小数，如：2，会在2后面补上00.即2.00  
function numFloat(x) {
    var f = parseFloat(x);
    if (isNaN(f)) {
        return false;
    }
    var f = Math.round(x * 100) / 100;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 2) {
        s += '0';
    }
    return s;
}
